package com.qianfeng.smartdevice.service.impl;


//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼            BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  


import com.qianfeng.smartdevice.pojo.Menu;
import com.qianfeng.smartdevice.pojo.User;
import com.qianfeng.smartdevice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jackiechan on 2021/5/24 22:49
 *
 * @author jackiechan
 */
@Service
public class MyUserDetailService implements UserDetailsService {
//    @Autowired
//    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    private UserService userService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
//        SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority("login");
//        grantedAuthorities.add(simpleGrantedAuthority);
//        User user = new User(username, bCryptPasswordEncoder.encode("123"), grantedAuthorities);
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        User userByUsername = userService.getUserWithPermisByUserName(username);
        if ("0".equals(userByUsername.getStatus())) {
            //用户被封了
            return new org.springframework.security.core.userdetails.User(username, "", null);
        }
        userByUsername.getRoleList().forEach(role -> {
            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority("ROLE_" + role.getName());
            grantedAuthorities.add(simpleGrantedAuthority);
            List<Menu> menuList = role.getMenuList();
            menuList.forEach(menu -> {
                if (!StringUtils.isEmpty(menu.getPerms())) {
                    SimpleGrantedAuthority simpleGrantedAuthority1 = new SimpleGrantedAuthority(menu.getPerms());
                    grantedAuthorities.add(simpleGrantedAuthority1);
                }

            });
        });
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        request.getSession().setAttribute("user", userByUsername);
        return new org.springframework.security.core.userdetails.User(username, userByUsername.getPassword(), grantedAuthorities);
    }
}
